using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace JufGame
{
	public class MyOctreeBuilder : MonoBehaviour
	{
		public GameObject[] Objects; //场景包含的全部对象
		public MyOctree Octree; // 八叉树
		[SerializeField] private bool isSeeOne = false; //是否只观察一个分裂后的节点
		[SerializeField] private bool isDrawOctreeCube = true; //是否绘制二叉树
		[SerializeField] private bool isDrawNode = true; // 是否要绘制图的节点
		[SerializeField] private bool isDrawEdge = true; // 是否要绘制图的边
		private void Awake()
		{
			Octree = new MyOctree(Objects, new MyGraph<MyOctreeNode, int>());
		}
		private void OnDrawGizmos()
		{
			if(Application.isPlaying)
			{
				if(isDrawOctreeCube)
				{
					Octree.RootNode.Draw(isSeeOne);
				}
				DrawGraph();
			}
		}
		private void DrawGraph()
		{
			if(isDrawEdge)
			{
				foreach(var edge in Octree.NavGraph.EdgeList)
				{
					Gizmos.color = Color.red;
					Gizmos.DrawLine(edge.Key.Item1.NodeCube.center, 
					edge.Key.Item2.NodeCube.center);
				}				
			}
			if(isDrawNode)
			{
				foreach(var node in Octree.NavGraph.NodeSet)
				{
					Gizmos.color = new Color(1, 1, 0);
					Gizmos.DrawWireSphere(node.NodeCube.center, 0.25f);
				}				
			}
		}
	}
}
